//
//  UIBarButtonItem+Extension.m
//  GiftTalk
//
//  Created by Wan on 16/3/22.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

/**
 *  系统原生导航栏按钮创建类方法
 *
 *  @param normalImage      显示图片(路径)
 *  @param target           action方法所属于的对象
 *  @param action           按钮绑定点击事件的方法名
 *
 *  @return 自定义导航栏按钮
 */
+ (instancetype)initWithImage:(NSString *)normalImage target:(id)target action:(SEL)action
{
    return [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:normalImage] style:UIBarButtonItemStyleDone target:target action:action];
}

/**
 *  自定义导航栏按钮创建类方法
 *
 *  @param normalImage      默认显示图片(路径)
 *  @param highlightedImage 高亮显示图片(路径)
 *  @param target           action方法所属于的对象
 *  @param action           按钮绑定点击事件的方法名
 *
 *  @return 自定义导航栏按钮
 */
+ (instancetype)itemWithImage:(NSString *)normalImage highlightedImage:(NSString *)highlightedImage target:(id)target action:(SEL)action
{
    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton setBackgroundImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [customButton setBackgroundImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    
    /** button 必须设置 size 否则不会显示*/
    customButton.size = customButton.currentBackgroundImage.size;
    /** 给按钮绑定点击事件*/
    [customButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[self alloc] initWithCustomView:customButton];
    
}

/**
 *  自定义导航栏按钮创建类方法
 *
 *  @param backgroundImage    默认背景图片(路径)
 *  @param highlightedBgImage 高亮背景图片(路径)
 *  @param target             action方法所属于的对象
 *  @param action             按钮绑定点击事件的方法名
 *
 *  @return 自定义导航栏按钮
 */
+ (instancetype)itemWithBackgroundImage:(NSString *)backgroundImage highlightedImage:(NSString *)highlightedBgImage target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage *bgImage = [[UIImage imageNamed:backgroundImage] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    UIImage *highlighted = [[UIImage imageNamed:highlightedBgImage] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    if (highlighted) {
        [button setBackgroundImage:highlighted forState:UIControlStateHighlighted];
    }
    
    return [[self alloc] initWithCustomView:button];
}


@end
